<?php
// 应用公共文件
use app\api\exception\ParameterException;
use think\facade\Lang;
use think\facade\Db;

/**
 *
 * 服务器算力转换
 * @param $hashrate 算力
 * @param $unit int
 * @param int $type 1 存 2 取
 * @return float|int|string
 */
function transformComputingUnit($hashrate, $unit, $type = 1)
{

    $unitList = [
        1 => 'h/s',
        2 => 'Kh/s',
        3 => 'Mh/s',
        4 => 'Gh/s',
        5 => 'Th/s',
        6 => 'Ph/s',
        7 => 'Eh/s',
    ];

    if (empty($hashrate)) {
        return '';
        //        throw new ParameterException([
        //            'message' => Lang::get('common.parameterError') . 'Hashrate'
        //        ]);
    }

    if (!isset($unitList['1'])) {
        throw new ParameterException([
//            'message' => Lang::get('common.parameterError') .'Type'
        ]);
    }

    switch ($type) {
            // 存 算换成最小值
        case 1:
            return $hashrate * pow(1000, $unit - 1);
            // 转换成实际值
        case 2:
            return ($hashrate / pow(1000, $unit - 1));
        default:
            throw new ParameterException([
                'message' => Lang::get('common.parameterError') . 'Type'
            ]);
    }
}

function generateUniqueString($length = 6)
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomBytes = random_bytes($length);
    $randomString = '';

    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[ord($randomBytes[$i]) % $length];
    }

    return $randomString;
}


/**
 * @describe 获取运营主管的ID,权限系统完善之后需要修改实际业务代码
 * @author: Bobo 
 * @date: 2024-12-17 11:59:01 
 * @return int
 */
function getOperationsSupervisor(): int
{
    $id = Db::table('db_operator')->where([
        'cname' => '潘志贤',
        'is_del' => 0,
    ])->value('id');
    return $id ?? 0;
}
